import Vue from "vue";
// import componentsMaps from "../../config/components";
import store from "../../store";
import {
    STYLE_PREFIX,
    REAL_PREFIX,
    EDIT_PREFIX
} from "../../config/components/prefix.js"; 
export function registerComponents() {
    if (window.environment !== "edit") return;
    let componentNameReg = /\.\/([a-zA-Z0-9\-_]+)\/.*/;
	const requireStyleComponent = require.context(
		// 其组件目录的相对路径
		'../../style-components',
		// 是否查询其子目录
		true,
		// 匹配基础组件文件名的正则表达式
		/_index\.vue$/
    );
    const requireRealComponent = require.context(
        "../../real-components",
        true,
        /_index\.vue$/
    )
    const requireEditComponent = require.context(
        "../../edit-components",
        true,
        /_index\.vue$/
    )
	requireStyleComponent.keys().forEach(fileName => {
		// 获取组件配置
        const componentConfig = requireStyleComponent(fileName);

		// let componentName = STYLE_PREFIX + fileName.match(componentNameReg)[1];
		let componentName = "StyleComponent";
		// console.log("截取组件名--->", t);
		// 全局注册组件
		Vue.component(
			componentName,
			// 如果这个组件选项是通过 `export default` 导出的，
			// 那么就会优先使用 `.default`，
			// 否则回退到使用模块的根。
			componentConfig.default || componentConfig
		);
	});
	requireRealComponent.keys().forEach(fileName => {
		// 获取组件配置
		const componentConfig = requireRealComponent(fileName);
		let componentName = REAL_PREFIX + fileName.match(componentNameReg)[1];
		// console.log("生成的组件名--->", componentName);
		// 全局注册组件
		Vue.component(
			componentName,
			componentConfig.default || componentConfig
		);
	});
	requireEditComponent.keys().forEach(fileName => {
		// 获取组件配置
		const componentConfig = requireEditComponent(fileName);
		let componentName = EDIT_PREFIX + fileName.match(componentNameReg)[1];
		// console.log("截取组件名--->", t);
		// 全局注册组件
		Vue.component(
			componentName,
			componentConfig.default || componentConfig
		);
	});
	console.log("打印目录下的东西", requireStyleComponent, requireStyleComponent.keys());
	console.log("打印目录下的东西", requireRealComponent, requireRealComponent.keys());
	console.log("打印目录下的东西", requireEditComponent, requireEditComponent.keys());
}